/**
 * Created by gongxun on 16/12/12.
 */

var WhmjCardHelper = function () {
    var that = new WhmjCardPlace();

    //合法的牌型
    that.allBasicLawCardNums = null;

    //赖子牌
    that.lastLaiziNum = null;
    that.laiziNum = null;
    that.piziNums = null;

    //单杠的牌
    that.singCardNums = [];

    //够胡
    that.ruleGouhu = null;
    that.ruleJinding = null;

    that.setCardScaleInfo = function () {
        // var width = mo.gameSize.x - that.inhandCardJiange * 2 - that.inhandCardOffset * 13;
        // var oneWidth = width / 14;
        // that.inhandCardScale = oneWidth / 73;

        that.init();

        //手牌可能性
        that.allBasicLawCardNums = [];
        for (var i = 0; i < 34; i++) {
            var num = i + 1;
            if (num <= 9) {
                num += 20;
            }
            else if (num <= 18) {
                num += (40 - 9);
            }
            else if (num <= 27) {
                num += (60 - 18);
            }
            else {
                num += (80 - 27);
            }
            that.allBasicLawCardNums.push(num);
        }
    };

    //返回开口牌结构 1 :eat  2 :peng 3 :pair 4:gang1明 5:gang2暗 6:gang3补 7:gang4单
    that.getOpenCardStruct = function (cards, type) {
        var openCard = {};

        openCard.cards = cards;
        openCard.type = type;

        return openCard;
    };

    //返回牌是否合法
    that.getCardNumIsBasicLaw = function (cardNum) {
        var isExist = false;
        for (var i = 0; i < that.allBasicLawCardNums.length; i++) {
            num = that.allBasicLawCardNums[i];
            if (cardNum === num) {
                isExist = true;
                break;
            }
        }
        return isExist;
    };

    //返回是否存在
    that.findCardForNum = function (cards, cardNum) {
        var rIndex = null;
        for (var i = 0; i < cards.length; i++) {
            if (cards[i].mCardNum == cardNum) {
                rIndex = i;
                break;
            }
        }
        return rIndex;
    };

    //返回随机手牌
    that.getRandomCardNum = function (size) {
        var cards = [];
        for (var i = 0; i < size; i++) {
            var num = parseInt(Math.random() * 34) + 1;
            if (num <= 9) {
                num += 20;
            }
            else if (num <= 18) {
                num += (40 - 9);
            }
            else if (num <= 27) {
                num += (60 - 18);
            }
            else {
                num += (80 - 27);
            }
            cards.push(num);
        }
        return cards;
    };

    //排序手牌
    that.sortCardForNum = function (cards) {
        cards.sort(function (a, b) {
            //让赖子牌放在最前面 皮子在之后
            if (a.mCardNum == that.laiziNum && b.mCardNum != that.laiziNum) {
                return -1;
            } else if (a.mCardNum != that.laiziNum && b.mCardNum == that.laiziNum) {
                return 1;
            }

            if (that.getIsPizi(a.mCardNum) && !that.getIsPizi(b.mCardNum)) {
                return -1;
            } else if (!that.getIsPizi(a.mCardNum) && that.getIsPizi(b.mCardNum)) {
                return 1;
            }

            //手牌num大的放在后面 如果一样就比较生成的id 防止跳牌
            var temp = (a.mCardNum - b.mCardNum);
            if (temp == 0) {
                return a.mCreatIndex - b.mCreatIndex;
            }
            return temp;
        });
    };

    //返回手牌坐标
    that.getInhandCardPos = function (seat, index, openCardSize, isFromCard) {
        if (seat == 0) {
            var xStart = that.inhandCardJiange;
            var offset = that.inhandCardOffset;
            var cardSize = cc.size(that.inhandCardSize.width * that.inhandCardScale, that.inhandCardSize.height * that.inhandCardScale);
            var lastCardAdd = (isFromCard ? that.inhandCardJiange / 2 : 0);
            var x = xStart + (cardSize.width + offset) * (index + openCardSize * 3) + lastCardAdd;
            return cc.p(x, 0);
        } else if (seat == 1) {
            var widthOffset = that.leftInhandRect.x;
            var xStart = mo.gameSize.x - widthOffset;
            var yAdd = (that.leftInhandCardSize.height + that.leftInhandCardOffset) * that.leftInhandCardScale;
            var yStart = that.leftInhandRect.y;
            var lastCardAdd = (isFromCard ? that.leftInhandCardOffsetLast : 0);
            var y = yStart + yAdd * (index + openCardSize * 3) + lastCardAdd;
            return cc.p(xStart, y);
        } else if (seat == 2) {
            var xAdd = (that.upInhandCardSize.width + that.upInhandCardOffset) * that.upInhandCardScale;
            var xStart = that.upInhandRect.x - xAdd * 1;
            var lastCardAdd = (isFromCard ? that.upInhandCardOffsetLast : 0);
            var x = xStart + xAdd * (index + openCardSize * 3) + lastCardAdd;
            var y = that.upInhandRect.y - that.upInhandCardSize.height * that.upInhandCardScale;
            return cc.p(x, y);
        } else if (seat == 3) {
            var widthOffset = that.leftInhandRect.x;
            var xStart = widthOffset;
            var yAdd = (that.leftInhandCardSize.height + that.leftInhandCardOffset) * that.leftInhandCardScale;
            var yStart = that.leftInhandRect.y + yAdd * 12;
            var lastCardAdd = (isFromCard ? that.leftInhandCardOffsetLast : 0);
            var y = yStart - yAdd * (index + openCardSize * 3) - lastCardAdd;
            return cc.p(xStart, y);
        }
    };


    //返回单杠牌的坐标
    that.getSingCardPos = function (seat, index) {

        var centerP = cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2);
        var xAdd = 0;
        var yAdd = 0;
        var yStart = 0;
        var xStart = 0;
        var pos = that.getDealCardPos(seat, 0);

        var cardSize = cc.size(0, 0);

        if (seat == 0) {
            cardSize = cc.size(that.inhandCardSize.width * that.dealScale, that.inhandCardSize.height * that.dealScale);

            xAdd = cardSize.width;

            xStart = pos.x - xAdd * 4 + xAdd * index;
            yStart = pos.y;

        } else if (seat == 1) {
            cardSize = cc.size(that.leftOpenCardSize.width * that.leftDealScale, that.leftOpenCardSize.height * that.leftDealScale);

            yAdd = cardSize.height -  25 * that.leftDealScale;

            xStart = pos.x;
            yStart = pos.y - yAdd * 4 + yAdd * index;

        } else if (seat == 2) {
            cardSize = cc.size(that.inhandCardSize.width * that.upDealScale, that.inhandCardSize.height * that.upDealScale);

            xAdd = cardSize.width;

            xStart = pos.x + xAdd * 4 - xAdd * index;
            yStart = pos.y;

        } else if (seat == 3) {
            cardSize = cc.size(that.leftOpenCardSize.width * that.leftDealScale, that.leftOpenCardSize.height * that.leftDealScale);

            yAdd = cardSize.height -  25 * that.leftDealScale;

            xStart = pos.x;
            yStart = pos.y + yAdd * 4 - yAdd * index;
        }

        return cc.p(xStart, yStart);
    },

        //返回打出牌的坐标
        that.getDealCardPos = function (seat, index) {
            var centerP = cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2);
            var xAdd = 0;
            var yAdd = 0;
            var yStart = 0;
            var xStart = 0;

            var cardSize = cc.size(0, 0);

            if (seat == 0) {
                cardSize = cc.size(that.inhandCardSize.width * that.dealScale, that.inhandCardSize.height * that.dealScale);

                xAdd = cardSize.width;
                yAdd = cardSize.height - 25 * that.dealScale;

                xStart = centerP.x - (xAdd * 11) / 2 + 2 * xAdd;
                yStart = that.inhandCardSize.height * that.inhandCardScale + 30;

                return cc.p(xStart + (index % 11) * xAdd, yStart + parseInt(index / 11) * yAdd);
            } else if (seat == 1) {
                cardSize = cc.size(that.leftOpenCardSize.width * that.leftDealScale, that.leftOpenCardSize.height * that.leftDealScale);

                xAdd = cardSize.width + 3;
                yAdd = cardSize.height -  25 * that.leftDealScale;

                yStart = centerP.y - (yAdd * 8) / 2 + yAdd;
                xStart = mo.gameSize.x - that.leftInhandRect.x - xAdd - that.leftInhandCardSize.width * that.leftInhandCardScale - 30;

                return cc.p(xStart - parseInt(index / 8) * xAdd, yStart + (index % 8) * yAdd);
            } else if (seat == 2) {
                cardSize = cc.size(that.upInhandCardSize.width * that.upDealScale, that.upInhandCardSize.height * that.upDealScale);

                xAdd = cardSize.width;
                yAdd = cardSize.height - 25 * that.upDealScale;

                xStart = centerP.x + (xAdd * 11) / 2 - 3 * xAdd;
                yStart = that.upInhandRect.y - that.upInhandCardSize.height * that.upInhandCardScale - yAdd - 20;

                return cc.p(xStart - (index % 11) * xAdd, yStart - parseInt(index / 11) * yAdd);
            } else if (seat == 3) {
                cardSize = cc.size(that.leftOpenCardSize.width * that.leftDealScale, that.leftOpenCardSize.height * that.leftDealScale);

                xAdd = cardSize.width + 3;
                yAdd = cardSize.height - 25 * that.leftDealScale;

                yStart = centerP.y + (yAdd * 8) / 2 - yAdd * 2;
                xStart = that.leftInhandRect.x + xAdd + 30;

                return cc.p(xStart + parseInt(index / 8) * xAdd, yStart - (index % 8) * yAdd);
            }

        };

    //返回开口牌坐标
    that.getOpenCardPos = function (seat, index, inIndex) {
        if (seat == 0) {
            var startPos = that.getInhandCardPos(seat, 0, index, false);
            startPos.y += 5;
            var openScale = that.openCardScale;
            var openOffset = 0;
            var xAdd = 0;
            var yAdd = 0;
            if (inIndex == 3){
                xAdd = (that.inhandCardSize.width * openScale + openOffset) * 1;
                yAdd = 34 * openScale;
            }else{
                xAdd = (that.inhandCardSize.width * openScale + openOffset) * inIndex;
            }
            return cc.p(startPos.x + xAdd, startPos.y + yAdd);
        } else if (seat == 1) {
            var pos = that.getInhandCardPos(seat, 0, index, false);
            var startY = pos.y;
            var x = pos.x - that.leftInhandCardSize.width * that.leftInhandCardScale;
            var y = 0;
            var scale = that.leftOpenCardScale;
            if (inIndex == 3){
                y = startY + 1 * 96 * scale + 33 * scale;
            }else{
                y = startY + inIndex * 96 * scale;
            }
            if (index > 0){
                y -= 6;
            }
            return cc.p(x, y);
        } else if (seat == 2) {
            var startPos = that.getInhandCardPos(seat, 0, index, false);
            startPos.y += 5;
            var openScale = that.upOpenCardScale;
            var openOffset = 0;
            var xAdd = 0;
            var yAdd = 0;
            if (inIndex == 3){
                xAdd = (that.upInhandCardSize.width * openScale + openOffset) * 1;
                yAdd = 34 * openScale;
            }else{
                xAdd = (that.upInhandCardSize.width * openScale + openOffset) * inIndex;
            }

            return cc.p(startPos.x + xAdd, startPos.y + yAdd);
        } else if (seat == 3) {
            var pos = that.getInhandCardPos(seat, 0, index, false);
            var startY = pos.y;
            var x = pos.x;
            var y = 0;
            var scale = that.leftOpenCardScale;
            if (inIndex == 3){
                y = startY - 1 * 96 * scale + 33 * scale;
            }else{
                y = startY - inIndex * 96 * scale;
            }
            if (index > 0){
                y += 6;
            }
            return cc.p(x, y);
        }

        return cc.p(0, 0);
    };

    that.setSingCards = function (singCards) {
        if (singCards == null || singCards.length == 0) {
            return;
        }

        that.singCardNums = [];
        that.piziNums = [];
        for (var i = 0; i < singCards.length; i++) {
            var singCardNum = singCards[i];
            that.singCardNums.push(singCardNum);

            if(mo.whmjTabel.mTableConfig.openrate == 2){
                if(singCardNum == that.laiziNum){
                    continue;
                }
                that.piziNums.push(singCardNum);
            }
        }
    };

    //生成赖子 和 皮子
    that.setLaiziCard = function (laiziCard) {
        //赖子
        that.laiziNum = laiziCard;
    };

    that.getIsSingCard = function (num) {
        if (!that.singCardNums) {
            return false;
        }
        return mo.gameHelper.findValueForArray(that.singCardNums, num) != null;
    };


    that.getIsLaizi = function (num) {
        return that.laiziNum == num;
    };

    that.getIsPizi = function (num) {
        if (!that.piziNums) {
            return false;
        }
        return mo.gameHelper.findValueForArray(that.piziNums, num) != null;
    };


    //返回手牌size
    that.getInhnadCardSize = function(){
        var cardSize = that.inhandCardSize;
        var cardSize = cc.size(cardSize.width * that.inhandCardScale, cardSize.height * that.inhandCardScale);
        return cardSize;
    };

    that.getDealCardSize = function () {
        var cardSize = that.inhandCardSize;
        var cardSize = cc.size(cardSize.width * that.dealScale, cardSize.height * that.dealScale);
        return cardSize;
    }


    //返回表情序列帧
    that.getFaceFrames = function (index) {
        var spriteFrames = [];
        for (var i = 0; i <= 60; i++){
            var frame = cc.spriteFrameCache.getSpriteFrame("emoji_" + index + "_" + i + ".png");
            if (frame == null){
                break;
            }
            spriteFrames.push(frame);
        }
        return spriteFrames;
    };


    return that;
};

mo.whmjCardHelper = new WhmjCardHelper();